export class IamUtil {
    private static readonly JWT = require('jwt-simple')
    private static secret: string = 'pfms'
    public static createToken(id: string): string {
        const expiresAt = new Date().setTime(new Date().getTime() + 1000 * 60 * 60 * 24)
        const playload = { id: id, expiresAt: expiresAt }
        return this.JWT.encode(playload, this.secret)
    }
    public static verifyToken(token: string, cb: Function): void {
        var decoded = this.JWT.decode(token, this.secret)
        if (parseFloat(decoded.expiresAt) > new Date().getTime() && decoded.id) {
            cb(TokenStatus.success)
        } else if (parseFloat(decoded.expiresAt) <= new Date().getTime()) {
            cb(TokenStatus.expired)
        }
    }
    public static getId(token: string, cb: Function): void {
        var decoded = this.JWT.decode(token, this.secret)
        cb(decoded.id)
    }
}
export enum TokenStatus { success, expired }